import { FormSchema } from '/@/components/BasicForm/type/form';
import { getComponentByApp, getPreferredByComponent } from '/@/api/system-mgmt/app-mgmt/component';
import { useMessage } from '/@/hooks/web/useMessage';

export const mirrorSchemas: FormSchema = [
  {
    field: 'id',
    show: false,
  },
  {
    field: 'appId',
    label: '应用id',
    component: 'Input',
    componentProps: ({ formActionType }) => {
      return {
        onChange: (e) => {
          const { updateSchema } = formActionType;
          updateSchema({
            field: 'componentId',
            componentProps: {
              api: getComponentByApp,
              params: { e },
              resultField: 'list',
              labelField: 'componentName',
              valueField: 'id',
              placeHolder: '请选择组件',
            },
          });
        },
      };
    },
    show: false,
  },
  {
    field: 'componentId',
    label: '组件',
    component: 'ApiSelect',
    componentProps: ({ formModel, formActionType }) => {
      return {
        api: getComponentByApp,
        params: {
          appId: formModel.appId,
          name: '',
        },
        onChange: async (e) => {
          const res = await getPreferredByComponent({
            componentId: e,
            appId: formModel.appId,
            id: formModel.id,
          });
          const { updateSchema, setFieldsValue } = formActionType;
          if (res.size === 0) {
            setFieldsValue({
              preferred: true,
            });
            updateSchema({
              field: 'preferred',
              dynamicDisabled: true,
            });
            const { createMessage } = useMessage();
            createMessage.info('首次添加组件镜像，必为优选。');
          } else {
            setFieldsValue({
              preferred: false,
            });
            updateSchema({
              field: 'preferred',
              dynamicDisabled: false,
            });
          }
        },
        resultField: 'list',
        labelField: 'name',
        valueField: 'id',
        placeHolder: '请选择组件',
      };
    },
    labelWidth: 100,
    colProps: { span: 24 },
    required: true,
  },
  {
    field: 'componentVersion',
    label: '组件版本',
    component: 'Input',
    componentProps: {
      placeHolder: '请输入组件版本',
    },
    labelWidth: 100,
    colProps: { span: 24 },
    required: true,
  },
  {
    field: 'name',
    label: '镜像名称',
    component: 'Input',
    componentProps: {
      placeHolder: '请输入镜像名称',
    },
    labelWidth: 100,
    colProps: { span: 24 },
    required: true,
  },

  {
    field: 'version',
    label: '镜像版本',
    component: 'Input',
    componentProps: {
      placeHolder: '请输入镜像版本',
    },
    labelWidth: 100,
    colProps: { span: 24 },
    required: true,
  },
  {
    field: 'organization',
    label: '所属组织',
    component: 'Input',
    componentProps: {
      placeHolder: '请输入所属组织',
    },
    labelWidth: 100,
    colProps: { span: 24 },
    required: true,
  },
  {
    field: 'address',
    label: '镜像仓库',
    component: 'Input',
    componentProps: {
      placeHolder: '请输入镜像仓库地址',
    },
    labelWidth: 100,
    colProps: { span: 24 },
    required: true,
  },
  {
    field: 'preferred',
    label: '是否优选',
    component: 'Switch',
    componentProps: {
      checkedChildren: '是',
      unCheckedChildren: '否',
      checkedValue: true,
      unCheckedValue: false,
      defaultChecked: false,
    },
    labelWidth: 100,
    colProps: { span: 24 },
    required: true,
  },
];
